/*
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is RhinoDB, released August 08, 2008.
 * 
 * Contributor(s): Alexandru Chiculita.
 * Alternatively, the contents of this file may be used under the terms of
 * the GNU General Public License Version 2 or later (the "GPL"), in which
 * case the provisions of the GPL are applicable instead of those above. If
 * you wish to allow use of your version of this file only under the terms of
 * the GPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replacing
 * them with the notice and other provisions required by the GPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the GPL.
 * 
 * ***** END LICENSE BLOCK ***** */

package org.achicu.db.logics;

import org.achicu.db.StorageException;
import org.achicu.db.Store;
import org.achicu.db.values.StorableInteger;
import org.achicu.db.values.StorableNumber;
import org.achicu.db.values.StorableObject;
import org.achicu.db.values.StorableValue;

public class FloatSortedCollectionLogic extends CollectionLogic{
	private static final String FLOAT_SORT_PREFIX = "<FloatSort>";
	
	private String sortingProperty = null; 
	
	public FloatSortedCollectionLogic(Store store, int id) {
		super(store, id);
	}

	public FloatSortedCollectionLogic(){ super(); }
	
	public FloatSortedCollectionLogic(Store store, String sortingProperty) {
		super(store);
		this.sortingProperty = sortingProperty;
		setConfig(getLogicConfig(sortingProperty));
	}
	
	
	
	public int getCollectionType(){
		return Store.FLOAT_INDEX;
	}
	
	
	public static String getLogicConfig(String sortingProperty){
		return FLOAT_SORT_PREFIX+sortingProperty;
	}
	
	
	
	public String getSortingProperty(){
		if(sortingProperty==null){
			String config = getConfig();
			
			if(config == null) 
				return null;
			
			sortingProperty = config.substring( FLOAT_SORT_PREFIX.length() );
		}
		return sortingProperty;
	}
	
	
	public boolean canAdd(StorableObject object) throws StorageException{
		StorableValue value = object.getProperty(getSortingProperty());
		return (value!=null && (value.getType()==StorableNumber.TYPE || value.getType()==StorableInteger.TYPE));
	}
	
	
	public void add(StorableObject object) throws StorageException{
		if(object.getProperty(getSortingProperty()).getType()==StorableNumber.TYPE){
			StorableNumber value = (StorableNumber) object.getProperty(getSortingProperty());
			store.addFloatCollectionItem(id, object.getId(), value.getValue());
		}else{
			StorableInteger intValue = (StorableInteger) object.getProperty(getSortingProperty());
			store.addFloatCollectionItem(id, object.getId(), intValue.getValue());
		}
	}
	
	public Integer[] getItems(StorableValue value, int skip, int limit){
		if(value!=null && ( value.getType()!=StorableNumber.TYPE && value.getType()!=StorableInteger.TYPE ) )
			return null;
		
		if(value.getType()==StorableNumber.TYPE){
			StorableNumber floatValue = (StorableNumber) value;
			return store.getFloatCollectionItems(id, floatValue.getValue(), skip, limit, this.reverse);
		}else{
			StorableInteger intValue = (StorableInteger) value;
			return store.getFloatCollectionItems(id, intValue.getValue(), skip, limit, this.reverse);
		}
	}
	
}
